package com.example.centertest;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Button bt1 = (Button)findViewById(R.id.button2);
        Button bt3 = (Button)findViewById(R.id.button3);
        EditText edt1 = (EditText)findViewById(R.id.editTextTextPersonName2);
        TextView tv1 =(TextView) findViewById(R.id.textView);
        Intent intentPre = this.getIntent();
        String zh = intentPre.getExtras().getString("zh");
        tv1.setText("你已成功登录账号"+zh);
        bt3.setOnClickListener((v)->{
            Intent intent = new Intent("messageToB2");
            intent.putExtra("message",edt1.getText().toString());

//            sendBroadcast(intent);
            Toast.makeText(this,"广播消息...",Toast.LENGTH_SHORT).show();
        });
        bt1.setOnClickListener((v)->{
            Intent intent = new Intent();
            Bundle bd=new Bundle();

            setResult(RESULT_OK,intent);
            this.finish();
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,"关于");
        menu.add(0,1,1,"退出");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        AlertDialog.Builder b=new AlertDialog.Builder(this);
        if(item.getItemId()==0){
            b.setTitle("关于");
            b.setMessage("朱传杰");
            b.setPositiveButton("确定",null);
            b.show();
        }else {
            b.setTitle("退出");
            b.setMessage("你确定要退出吗");
            b.setPositiveButton("确定", (dialog, which) -> {
                finish();
            });
            b.setNegativeButton("取消",null);
        }
        return super.onOptionsItemSelected(item);
    }

}